package cn.bigcontainer.alt.meta.controller;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import cn.bigcontainer.alt.common.exception.BigResultCode;
import cn.bigcontainer.alt.common.util.BigConstant;
import cn.bigcontainer.alt.meta.dto.MDictionaryDto;
import cn.bigcontainer.alt.meta.service.MDictionaryService;

@Controller
@RequestMapping("/meta/dictionary")
@SessionAttributes(BigConstant.SESSION_USER)
public class MDictionaryController {

	@Resource
	private MDictionaryService dictionaryService;

	/**
	 * 根据类型获取数据字段集合
	 * @param type
	 * @param values 设置需要设置为active的数据字典id
	 * @return
	 */
	@RequestMapping
	@ResponseBody
	public BigResultCode getJson(String type, String[] values) {
		List<MDictionaryDto> dictionaries = dictionaryService.getDictionaries(type);
		if (values != null && values.length > 0) {
			if (!Arrays.asList("", "-1").contains(values[0])) {
				dictionaries.forEach(d -> {
					d.setActive(Arrays.asList(values).contains(d.getValue()));
				});
			} else if ("-1".equals(values[0])) {
				if (dictionaries.size() > 0) {
					dictionaries.get(0).setActive(true);
				}
			}
		}
		return BigResultCode.getSuccessCode().setContent(dictionaries);
	}

}
